package components;


public class Variable {
	
	private final String name;
	private final int domainSize;
	private final int ignorable;
	private final int index;
	private boolean init;
	private int initVal;
	private int state;
	private int agent;
	private boolean isPublic;
	
	public Variable(String _name,int _domainSize,int _ignorable,int i){
		name=_name;
		domainSize=_domainSize;
		ignorable=_ignorable;
		agent=-1;
		init=false;
		index=i;
		isPublic=false;
	}
	
	/**
	 * setter
	 * @param x
	 * @throws Exception
	 */
	public void setState (int x) throws Exception{
		if (x<0 || x>domainSize)
			throw new Exception("state given to var "+name+" is out of bounds");
		else{
			state=x;
			if (!init){init=true; initVal=x;}
		}
	}
	/**
	 * getter
	 * @return
	 */
	public int getState(){
		return state;
	}
	/**
	 * getter
	 * @return
	 */
	public int getDomain(){return domainSize;}


	public String toString() {
		return "VARIABLE:"+name+" "+domainSize+" "+ignorable+" "+agent;
	}
	/**
	 * output string
	 * @return
	 * @throws Exception if agent didnt setted yet so variable not ready to be written
	 */
	public String toString2() throws Exception {
		//TODO remove comment mark 
		//if (agent==-1) throw new Exception ("variable not ready to write yet");
		return name+" "+domainSize+" "+ignorable+" "+agent+"\n";
	}
	
	
/**
 * getter	
 * @return
 */
	public int getInitVal(){
		return initVal;
	}

/**
 * setter
 * @return
 */
	public int getIndex() {
		return index;
	}

	
	/**
	 * sets the agent of the var
	 * @param x
	 */
	public void setAgent(int x){
		if (!isPublic)
			if (this.agent==-1){
				this.agent=x;
			}
			else
				if (this.agent!=x){
					this.agent=0;
					this.isPublic=true;
				}
	}
	
	
	
	
	
	

}
